<template>
	<view>
		<template v-if="searchList.length === 0">
			<!-- 搜索历史 -->
			<view class="py-2 font-md px-2">搜索历史</view>
			<view class="flex flex-wrap">
				<view 
				class="border rounded font mx-2 my-1 px-2"
				v-for="(item,index) in list" 
				:key="index" 
				hover-class="bg-light"
				@click="ClicSearchHistory(item)">{{item}}</view>
			</view>
			
		</template>
		<template v-else>
			<!-- 数据列表 -->
			<block v-for="(item,index) in searchList" :key="index">
				<template v-if="type==='post'">
					<common-list @doSupport="doSupport" @Follow="Follow" :item="item" :index="index"></common-list>
				</template>
				<template v-else-if="type==='topic'">
					<topic-list :item="item" :index="index"></topic-list>
				</template>
				<template v-else>
					<user-list :item="item" :index="index"></user-list>
				</template>
			</block>
		</template>
	</view>
</template>

<script>
	import CommonList from '@/components/common/common-list'
	import userList from '@/components/user-list/user-list.vue'
	import TopicList from '@/components/news/topic-list';
	import {getSearch} from "@/network/follow.js"
	
	
	export default{
		components:{
			CommonList,
			userList,
			TopicList
		},
		onLoad(e) {
			
			if(e.type){
				this.type = e.type
			}
			let pageTitle = '帖子'
			switch(e.type){
				case 'post':
				pageTitle = '帖子'
					break;
				case 'topic':
				pageTitle = '话题'
					break;
				case 'user':
				pageTitle = '用户'
					break;
			}
			// 取出搜索历史
			let list = uni.getStorageSync('historySeachText')
			if(list){
				this.list = JSON.parse(list)
			}
			// 监听关注和顶踩
			uni.$on("updateFollowOrSupport",(e)=>{
				switch (e.type){
					case "follow":
					this.Follow(e.data.user_id)
						break;
					case "support":
					this.doSupport(e.data.news_list)
						break;
					case "comment":
					this.doComment(e.data.comment)
						break;
				}
			})
			// 修改搜索占位
			// #ifdef APP-PLUS
					let currentWebbiew = this.$mp.page.$getAppWebview();
					let tn = currentWebbiew.getStyle().titleNView;
					if(tn===undefined){
						return
					}
					tn.searchInput.placeholder ='搜索'+pageTitle;
					currentWebbiew.setStyle({
						titleNView:tn
					})
			// #endif
		
			
		},
		data(){
			return{
				searchList:[],
				searchText:"",
				type:'post',
				list:[]
			}
		},
		// 监听导航输入
		onNavigationBarSearchInputChanged(e){
			this.searchText = e.text
		},
		// 监听搜索按钮
		onNavigationBarButtonTap(e){
			if(e.index===0){
				this.serarchEvent()
			}
		},
		onUnload() {
			uni.$off("updateFollowOrSupport",(e)=>{})
		},
		methods:{
			// 搜索历史
			ClicSearchHistory(text){
				this.searchText = text
				this.serarchEvent()
			},
			// 搜索事件
			serarchEvent(){
				// 收起键盘
				uni.hideKeyboard()
				let index = this.list.findIndex(v => v===this.searchText)
				if(index !== -1){
					this.$U.__toFirst(this.list,index)
				} else {
					this.list.unshift(this.searchText)
				}
				uni.setStorageSync('historySeachText',JSON.stringify(this.list))
				uni.showLoading({
					title:'加载中...',
					mask:false
				})
				// 请求搜索
				setTimeout(()=>{
					getSearch(this.searchText,this.type).then(attr=>{
						let [err,res] = attr
						console.log(res)
						this.searchList = res.data.msg
					})	
					//隐藏loding
					uni.hideLoading()
				},2000)
			},
			// 关注功能
			Follow(user_id){
				this.searchList.forEach(item=>{
					if(item.user==user_id){
						item.isFollow = !item.isFollow
					}
					return
				})
			},
			// 评论
			doComment(e){
				this.searchList.forEach(item=>{
					if(item.id==e.id){
						item.comment_count = e.count
					}
					return
				})
			},
			// 顶踩功能
			doSupport(news_list){
					let types = news_list.type
					let msg = types==="support" ? "点赞" : "点踩"
					this.searchList.forEach(item=>{
						if(item.id==news_list.id){
							// 如果已点过则进行取消操作
							if(item.news_list.type===types){
								item.news_list[types+"_count"]--
								item.news_list.type=""
								uni.showToast({title:"取消成功"})
								return
							}
							// 如果没点过取消，且没选中任何功能，则执行
							if(item.news_list.type){
								let my_type = item.news_list.type
								item.news_list[my_type+"_count"]--
							}
							// 如果什么都没选执行
							item.news_list.type=types
							item.news_list[types+"_count"]++
							console.log(item)
							uni.showToast({title:msg+"成功"})
						}
						return
					})
			},
		}
	}
</script>

<style>
</style>
